﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan.DataLayer
{
    class NganHangDoiTacDAO:BaseDAO
    {
        // save into DB, @id = -1: insert else, update
        public ResultObj SaveNganHangDoiTac(NganHangDoiTac nganhangdoitac)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spAddNganHangDoiTac", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spAddNganHangDoiTac";
            SqlParameter param = new SqlParameter();
            param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, nganhangdoitac.Id);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, nganhangdoitac.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@Ten", DbType.String, ParameterDirection.Input, nganhangdoitac.Ten);
            cmd.Parameters.Add(param);
            param = CreateParameter("@DiaChi", DbType.String, ParameterDirection.Input, nganhangdoitac.DiaChi);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdNCC", DbType.Int32, ParameterDirection.Input, nganhangdoitac.IdNCC);
            cmd.Parameters.Add(param);
            param = CreateParameter("@IdKhachHang", DbType.Int32, ParameterDirection.Input, nganhangdoitac.IdKhachHang);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
            conObj.Open();
            cmd.ExecuteNonQuery();
            conObj.Close();
            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);

            return result;
        }

        // Get all NganHangDoiTac in DB
        public ResultObj GetAllNganHangDoiTac()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<NganHangDoiTac> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllNganHangDoiTac", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<NganHangDoiTac>();
                NganHangDoiTac item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new NganHangDoiTac();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.Ten = Convert.ToString(reader["Ten"]);
                    item.DiaChi = Convert.ToString(reader["DiaChi"]);
                    item.IdNCC = Convert.ToInt32(reader["IdNCC"]);
                    item.IdKhachHang = Convert.ToInt32(reader["IdKhachHang"]);
                    
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Get all NganHang in DB with condition
        public ResultObj GetAllNganHangDoiTacByID(NganHangDoiTac nganhangdoitac)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<NganHangDoiTac> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllNganHangDoiTacByID", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, nganhangdoitac.Id);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<NganHangDoiTac>();
                NganHangDoiTac item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new NganHangDoiTac();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.Ten = Convert.ToString(reader["Ten"]);
                    item.DiaChi = Convert.ToString(reader["DiaChi"]);
                    item.IdNCC = Convert.ToInt32(reader["IdNCC"]);
                    item.IdKhachHang = Convert.ToInt32(reader["IdKhachHang"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Delete a NganHangDoiTac out of DB with condition
        public ResultObj DeleteNganHangDoiTac(NganHangDoiTac nganhangdoitac)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spDeleteNganHangDoiTac", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spDeleteNganHangDoiTac";
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, nganhangdoitac.Id);
            cmd.Parameters.Add(param);
            conObj.Open();
            result.ReturnCode = cmd.ExecuteNonQuery();
            conObj.Close();

            return result;
        }
    }
}
